package com.alks.function.service.impl.produce;

import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.UserInfoUtil;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.entity.PcSalaryDept;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.mapper.finance.salary.PcSalaryDeptMapper;
import com.alks.function.service.finance.salary.PcSalaryDeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class PcSalaryDeptServiceImpl extends ServiceImpl<PcSalaryDeptMapper, PcSalaryDept> implements PcSalaryDeptService {

    @Autowired
    private PcSalaryDeptMapper pcSalaryDeptMapper;

    /**
     * 管理组别建档-查询
     * @return
     */
    @Override
    public ResponseInfo PcSalaryDeptList() {
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        LambdaQueryWrapper<PcSalaryDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcSalaryDept::getCompanyId,userInfo.getCompanyId())
                .orderByAsc(PcSalaryDept::getDeptName,PcSalaryDept::getDeptName2);
        List<PcSalaryDept> list= pcSalaryDeptMapper.selectList(wrapper);
        return ResponseInfo.ok(list);
    }

    /**
     * 管理组别建档-修改
     * @param entity
     * @return
     */
    @Override
    public ResponseInfo updatePcSalaryDept(PcSalaryDept entity) {
        if (ObjectUtils.isEmpty(entity.getId())){
            return ResponseInfo.error("id不能为空");
        }
        if (ObjectUtils.isEmpty(entity.getDeptName())){
            return ResponseInfo.error("管理组别不能为空");
        }
        if (ObjectUtils.isEmpty(entity.getDeptName2())){
            return ResponseInfo.error("管理分组名称不能为空");
        }
        if (ObjectUtils.isEmpty(entity.getRate())){
            return ResponseInfo.error("提成比率不能为空");
        }
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        entity.setSysUser(userInfo.getUserName());
        entity.setSysDate(LocalDateTime.now());
        LambdaUpdateWrapper<PcSalaryDept> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PcSalaryDept::getId,entity.getId())
                .eq(PcSalaryDept::getCompanyId,userInfo.getCompanyId());
        pcSalaryDeptMapper.update(entity,wrapper);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 管理组别建档-新增
     * @param entity
     * @return
     */
    @Override
    public ResponseInfo addPcSalaryDept(PcSalaryDept entity) {
        if (ObjectUtils.isEmpty(entity.getDeptName())){
            return ResponseInfo.error("管理组别不能为空");
        }
        if (ObjectUtils.isEmpty(entity.getDeptName2())){
            return ResponseInfo.error("管理分组名称不能为空");
        }
        if (ObjectUtils.isEmpty(entity.getRate())){
            return ResponseInfo.error("提成比率不能为空");
        }
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        entity.setCompanyId(userInfo.getCompanyId());
        entity.setSysUser(userInfo.getUserName());
        entity.setSysDate(LocalDateTime.now());
        pcSalaryDeptMapper.insert(entity);
        return ResponseInfo.ok("操作成功!");
    }

    @Override
    public ResponseInfo delPcSalaryDept(String id) {
        if (ObjectUtils.isEmpty(id)){
            return ResponseInfo.error("id不能为空!");
        }
        LambdaUpdateWrapper<PcSalaryDept> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PcSalaryDept::getId,id).eq(PcSalaryDept::getCompanyId, UserIdThread.get().getCompanyId());
        pcSalaryDeptMapper.delete(wrapper);
        return ResponseInfo.ok("操作成功!");
    }
}
